home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 283_01 / savescrn.c < prev    next >
C/C++ Source or Header  |  1988-12-15  |  5KB  |  195 lines

  1. /* savescrn.c -- 9/5/88, d.c.oshel
  2. */
  3.  
  4. #include "vidinit.h"
  5.  
  6.  
  7. /* savecursor() and restcursor() save all aspects of the current window
  8. ** and cursor environment, but do not save or restore screen contents;
  9. ** both return the value of the save/restored synchronized flag
  10. */
  11.  
  12. int savecursor( union REGS *x )
  13. {
  14.     if (!Initialized) vid_init(0);
  15.  
  16.     x->h.ah = 3;            /* read machine cursor into dh, dl */
  17.     x->h.bh = 0;
  18.     int86( 0x10, x, x );
  19.     x->h.bh    = synchronized;
  20.     x->h.bl    = vid_attr;
  21.     x->x.ax    = (tm * 256) + bm;             /* save window */
  22.     x->x.di    = (lm * 256) + rm;
  23.     x->x.si    = (row * 256) + col;           /* save soft cursor */
  24.     return (synchronized);
  25. }
  26.  
  27.  
  28. int restcursor( union REGS *x )
  29. {
  30.     union REGS rx;
  31.  
  32.     if (!Initialized) vid_init(0);
  33.  
  34.     /* restore user's window, soft cursor row & col, synch flag, vid_attr */
  35.  
  36.     tm = bm = x->x.ax;
  37.     tm /= 256;
  38.     bm %= 256;
  39.  
  40.     lm = rm = x->x.di;
  41.     lm  /= 256;
  42.     rm  %= 256; 
  43.  
  44.     row = col = x->x.si;
  45.     row /= 256;
  46.     col %= 256;
  47.  
  48.     synchronized = x->h.bh;
  49.     vid_attr = x->h.bl;
  50.  
  51.     /* restore machine cursor size and location */
  52.  
  53.     rx.h.ah = 1;
  54.     rx.x.bx = 0;                /* bh always has page 0, bl unused  */
  55.     rx.x.cx = x->x.cx;          /* set old cursor size     */
  56.     int86( 0x10, &rx, &rx );
  57.     rx.h.ah = 2;
  58.     rx.x.bx = 0;                /* always page 0 */
  59.     rx.x.dx = x->x.dx;          /* dh has row, dl has col (machine's!) */
  60.     int86( 0x10, &rx, x );      /* set old cursor position */
  61.     return (synchronized);
  62. }
  63.  
  64.  
  65. /*
  66. **  ================================================
  67. **  Sample calls to savescreen(), restorescreen()
  68. **  Note:  These only work with text screens, 
  69. **  they do not save CRTpage/CPUpage screen info.
  70. **  They DO save the current window margins!
  71. **  ================================================
  72. **
  73. **   main() {
  74. **
  75. **   union REGS x,z;
  76. **   char far *screen1, *screen2;
  77. **
  78. **   vid_init();                            WARNING: must call vid_init()
  79. **   screen1 = savescreen( &x );
  80. **   .
  81. **   .
  82. **          screen2 = savescreen( &z );     WARNING: these MUST nest properly
  83. **          .                                        or heap allocation will
  84. **          .                                        be annihilated! 
  85. **          .
  86. **          restorescreen( screen2, &z );
  87. **   .
  88. **   .
  89. **   restorescreen( screen1, &x );
  90. **   }
  91. **
  92. */
  93.  
  94. static void sorry( int n )
  95. {
  96.     char *p,*q;
  97.     q = "init";
  98.     switch (n)
  99.     {
  100.     case 2:  q = "restorescreen"; p = "no screen saved, can't restore"; break;
  101.     case 1:  q = "savescreen";
  102.     case 0:  p = "not enough k, can't continue"; break;
  103.     default: q = "nasty"; p = "looks fatal"; break;
  104.     } 
  105.     printf("\n\aCIAO: %s error: %s\n",q,p);
  106.     exit(1);
  107. }
  108.  
  109.  
  110.  
  111. /* caller must keep pointer to storage! */
  112. char far *savescreen( union REGS *cursor )
  113. {
  114.     union REGS ry;
  115.     char far *screen;
  116.      
  117.     if (!Initialized) vid_init(0);
  118.  
  119.     ry.h.ah = 0x03;                   /* read cursor position...       */
  120.     ry.h.bh = 0;                      /* text page 0, only             */
  121.     int86( 0x10, &ry, cursor );       /* store result in caller's var  */
  122.  
  123.     /* cannot use cx, dx; bh is always 0, bl is unused */
  124.  
  125.     cursor->x.cflag = 0xDC;                        /* save OKflag */
  126.     cursor->h.bh    = synchronized;
  127.     cursor->h.bl    = vid_attr;
  128.     cursor->x.ax    = (tm * 256) + bm;             /* save window */
  129.     cursor->x.di    = (lm * 256) + rm;
  130.     cursor->x.si    = (row * 256) + col;           /* save soft cursor */
  131.  
  132.     screen = (char far *) _fmalloc( 4000 );
  133.     if ( screen == (char far *) NULL )
  134.     {
  135.         sorry(1);
  136.     }
  137.     MSJ_MovScrBuf( screen, 0, 0, 2000, &video );
  138.  
  139.     return (screen);
  140. }
  141.  
  142.  
  143.  
  144.  
  145. /* caller keeps pointer to storage! */
  146. void restorescreen( char far *screen, union REGS *cursor )
  147. {
  148.     extern int ScrollAttributeFlag;  /* in scroll.c */
  149.     union REGS rx;
  150.  
  151.     if (!Initialized || (cursor->x.cflag != 0xDC))  /* 0xDC means screen saved */
  152.     {
  153.         sorry(2);
  154.     }
  155.  
  156.  
  157.     MSJ_MovBufScr( screen, 0, 0, 2000, &video );
  158.     _ffree( screen );
  159.  
  160.     ScrollAttributeFlag = 0;
  161.  
  162.     /* restore user's window, soft cursor row & col, synch flag, vid_attr */
  163.  
  164.     tm = bm = cursor->x.ax;
  165.     tm /= 256;
  166.     bm %= 256;
  167.  
  168.     lm = rm = cursor->x.di;
  169.     lm  /= 256;
  170.     rm  %= 256; 
  171.  
  172.     row = col = cursor->x.si;
  173.     row /= 256;
  174.     col %= 256;
  175.  
  176.     synchronized = cursor->h.bh;
  177.     vid_attr = cursor->h.bl;
  178.  
  179.     /* reset, especially, OKflag = 0 in cflag! */
  180.  
  181.     cursor->x.ax = cursor->x.si = cursor->x.di = cursor->x.cflag = 0;
  182.  
  183.     /* restore machine cursor size and location */
  184.  
  185.     rx.h.ah = 1;
  186.     rx.x.bx = 0;                /* bh always has page 0, bl unused  */
  187.     rx.x.cx = cursor->x.cx;     /* set old cursor size     */
  188.     int86( 0x10, &rx, &rx );
  189.     rx.h.ah = 2;
  190.     rx.x.bx = 0;                /* always page 0 */
  191.     rx.x.dx = cursor->x.dx;     /* dh has row, dl has col (machine's!) */
  192.     int86( 0x10, &rx, cursor ); /* set old cursor position */
  193. }
  194.  
  195.